﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Diagnostics;
using System.Windows.Forms;
using Contract;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace Service
{
    public partial class frmServer : Form
    {
        ICallBack Proxy;
        public frmServer()
        {
            InitializeComponent();

            // create a hosting process for CalculatorService
            ServiceHost host = new ServiceHost(typeof(DataUploader));

            NetTcpBinding binding = new NetTcpBinding();
            binding.TransferMode = TransferMode.Streamed;
            binding.ReceiveTimeout = TimeSpan.MaxValue;
            binding.MaxReceivedMessageSize = long.MaxValue;
            host.AddServiceEndpoint(typeof(IDataUploader), binding,
            new Uri("net.tcp://" + Dns.GetHostAddresses(Environment.MachineName)[Dns.GetHostAddresses(Environment.MachineName).Length - 2] + ":5000/DataUploader"));
           
            host.Open();
            label1.Text = "Service is started.";

            // Create proxy                                           Ip of agent allocator
            EndpointAddress address = new EndpointAddress("net.tcp://"+Dns.GetHostAddresses(Environment.MachineName)[Dns.GetHostAddresses(Environment.MachineName).Length - 2]+":8888/Service_Allocation");
            ChannelFactory<IService_Allocation> channel = new ChannelFactory<IService_Allocation>(binding, address);
            IService_Allocation Proxy = channel.CreateChannel();
            Proxy.registerAgent(Dns.GetHostAddresses(Environment.MachineName)[Dns.GetHostAddresses(Environment.MachineName).Length - 2]);
        }

        private void frmServer_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //////////////////////////////////////
            NetTcpBinding binding = new NetTcpBinding();
            binding.TransferMode = TransferMode.Streamed;
            binding.SendTimeout = TimeSpan.MaxValue;
            binding.MaxReceivedMessageSize = 1000;
            binding.PortSharingEnabled = true;

            EndpointAddress address = new EndpointAddress("net.tcp://1:8001/CallBack");

            ChannelFactory<ICallBack> channel = new ChannelFactory<ICallBack>(binding, address);
            Proxy = channel.CreateChannel();
            ///////////////////////////////////////////////
        }
    }
}
